package com.emapp.lccode.activity

import android.app.DatePickerDialog.OnDateSetListener
import android.os.Bundle
import android.view.inputmethod.EditorInfo
import android.widget.DatePicker
import androidx.lifecycle.Observer
import com.emapp.lccode.R
import com.emapp.lccode.adapter.ProjectAdapter
import com.emapp.lccode.base.BaseActivity1
import com.emapp.lccode.base.BaseConfig
import com.emapp.lccode.base.EventBusConfig
import com.emapp.lccode.base.EventBusModel
import com.emapp.lccode.bean.Project
import com.emapp.lccode.databinding.ActivityProjectListBinding
import com.emapp.lccode.mode.ProjectViewModel
import com.emapp.lccode.utils.setOnclickNoRepeat
import com.emapp.lccode.utils.vertical
import com.gyf.immersionbar.ktx.immersionBar
import com.hjq.toast.Toaster
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import me.hgj.jetpackmvvm.ext.parseState
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**项目
 */
class ProjectListActivity : BaseActivity1<ProjectViewModel, ActivityProjectListBinding>(),
    OnDateSetListener {
    var datas: ArrayList<Project> = arrayListOf()
    private val projectAdapter: ProjectAdapter by lazy { ProjectAdapter() }
    private var keywords = ""
    var page = 1
    override fun initView(savedInstanceState: Bundle?) {
        immersionBar {
            titleBar(mViewBind.lvTitle.ivBar)
            statusBarDarkFont(true)
            fullScreen(true)
            statusBarColor(R.color.transparent)
            navigationBarColor(R.color.white)
        }
        mViewBind.lvTitle.tvTitle.text = "项目列表"
        mViewBind.lvTitle.tvRight.text = ""
        EventBus.getDefault().register(this)
        mViewBind.rvList.run {
            vertical()
            adapter = projectAdapter
        }
        projectAdapter.run {
            setOnItemClickListener { adapter, _, position ->
                val item = adapter.items[position]
            }
            isEmptyViewEnable = true
            emptyView = layoutInflater.inflate(R.layout.listitem_empty, mViewBind.rvList, false)
            submitList(datas)
        }
        mViewBind.refreshLayout.run {
            setRefreshHeader(ClassicsHeader(this@ProjectListActivity))
            setRefreshFooter(ClassicsFooter(this@ProjectListActivity))
            setEnableLoadMore(false)
            setOnRefreshListener {
                page = 1
                getList()
            }
            setOnLoadMoreListener {
                page++
                getList()
            }
        }
        showLoading()
        getList()
        mViewModel.getDeviceAllType("0")
        mViewBind.tvSearch.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                keywords = v.text.toString().trim()
                showLoading()
                page = 1
                getList()
                true
            } else {
                false
            }
        }
    }

    private fun getList() {
        mViewModel.getUnFinishuProjectList(keywords, page);
    }

    override fun createObserver() {
        mViewModel.run {
            projectList.observe(this@ProjectListActivity, Observer { resultState ->
                parseState(resultState, {
                    if (page == 1) {
                        datas.clear()
                        if (it != null) {
                            datas.addAll(it)
                            if (it.size < BaseConfig.page_count) {
                                mViewBind.refreshLayout.finishRefreshWithNoMoreData()
                            } else {
                                mViewBind.refreshLayout.finishRefresh()
                            }
                        } else {
                            mViewBind.refreshLayout.finishRefreshWithNoMoreData()
                        }
                    } else {
                        if (it != null) {
                            datas.addAll(it)
                            if (it.size < BaseConfig.page_count) {
                                mViewBind.refreshLayout.finishLoadMoreWithNoMoreData()
                            } else {
                                mViewBind.refreshLayout.finishLoadMore()
                            }
                        } else {
                            mViewBind.refreshLayout.finishLoadMoreWithNoMoreData()
                        }
                    }
                    projectAdapter.notifyDataSetChanged()
                }, {
                    Toaster.show(it.errorMsg)
                    mViewBind.refreshLayout.finishRefreshWithNoMoreData()
                })
            })
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    override fun onBindViewClick() {
        setOnclickNoRepeat(
            mViewBind.lvTitle.ivLeft,
            mViewBind.btSearch,
            mViewBind.lvTitle.tvRight
        ) {
            when (it.id) {
                R.id.iv_left -> {
                    finish()
                }

                R.id.tv_right -> {
                }

                R.id.bt_search -> {
                    keywords = mViewBind.tvSearch.text.toString().trim()
                    showLoading()
                    page = 1
                    getList()
                }
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(event: EventBusModel) {
        when (event.type) {
            EventBusConfig.FINISH -> {
            }

            else -> {}
        }
    }


    override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
    }

}
